package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxDeptList;
import com.adk.backend.mapper.DeptListMapper;
import com.adk.backend.service.DeptListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 部门列表服务实现类
 */
@Slf4j
@Service
public class DeptListServiceImpl extends ServiceImpl<DeptListMapper, LxDeptList> implements DeptListService {
    
    @Autowired
    private DeptListMapper deptListMapper;
    
    @Override
    public PageResult<LxDeptList> getDeptList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        QueryWrapper<LxDeptList> wrapper = new QueryWrapper<>();
        
        // 按公司ID筛选
        if (params.get("companyId") != null) {
            wrapper.eq("company_id", params.get("companyId"));
        }
        
        // 按父级ID筛选
        if (params.get("parentId") != null) {
            wrapper.eq("parent_id", params.get("parentId"));
        }
        
        // 按部门名称模糊查询
        if (params.get("deptName") != null && !params.get("deptName").toString().isEmpty()) {
            wrapper.like("dept_name", params.get("deptName"));
        }
        
        wrapper.orderByAsc("id");
        
        Page<LxDeptList> page = new Page<>(pageNum, pageSize);
        Page<LxDeptList> result = deptListMapper.selectPage(page, wrapper);
        
        PageResult<LxDeptList> pageResult = new PageResult<>();
        pageResult.setList(result.getRecords());
        pageResult.setTotal(result.getTotal());
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages((int) result.getPages());
        
        return pageResult;
    }
    
    @Override
    public LxDeptList getDeptById(Long id) {
        return deptListMapper.selectById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxDeptList createDept(LxDeptList deptList) {
        deptListMapper.insert(deptList);
        return deptList;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDept(LxDeptList deptList) {
        return deptListMapper.updateById(deptList) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDept(Long id) {
        return deptListMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<LxDeptList> getAllDeptList(Integer companyId) {
        QueryWrapper<LxDeptList> wrapper = new QueryWrapper<>();
        
        if (companyId != null) {
            wrapper.eq("company_id", companyId);
        }
        
        wrapper.orderByAsc("id");
        
        return deptListMapper.selectList(wrapper);
    }
    
    @Override
    public List<LxDeptList> getDeptListByParentId(Long parentId) {
        QueryWrapper<LxDeptList> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        wrapper.orderByAsc("id");
        return deptListMapper.selectList(wrapper);
    }
}

